package edu.uestc.gene.scripts.combination.entity;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import edu.uestc.gene.entity.Drug;
import edu.uestc.gene.entity.Gene;

/**
 * @author Carl
 * 
 *         2013-10-30
 */
public class CombinedGene extends Gene {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected String name;

	private List<Gene> genes;

	/**
	 * 
	 */
	public CombinedGene(String name) {
		super();
		genes = new ArrayList<Gene>();
		this.name = name;
	}

	/**
	 * 将基因组合并为一个新的基因<br>
	 * 1.将所有基因的药物合并<br>
	 * 2.对应药物的基因集合修改
	 */
	public void combine() {
		// 合并所有药物
		Set<Drug> combined = new HashSet<Drug>();
		for (int i = 0; i < genes.size(); i++) {
			combined.addAll(genes.get(i).getDrugs());
		}
		// 修改每个药物的基因集合
		drugs.clear();
		drugs.addAll(combined);
		for (int i = 0; i < drugs.size(); i++) {
			Drug d = drugs.get(i);
			d.removeGenes(genes);
			d.addGene(this);
		}
	}

	public boolean isExist(Gene gene) {
		return genes.contains(gene);
	}

	public List<Gene> getGenes() {
		return genes;
	}

	public void setGenes(List<Gene> genes) {
		this.genes = genes;
	}

	public void addGene(Gene gene) {
		genes.add(gene);
	}

	public void removeGene(Gene gene) {
		genes.remove(gene);
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}
